package api

import (
	"api-gateway/basic/global"
	"api-gateway/basic/proto/user"
	"api-gateway/handler/req"
	"api-gateway/pkg"
	"github.com/dgrijalva/jwt-go"
	"github.com/gin-gonic/gin"
	"net/http"
	"time"
)

// 登录
func Login(c *gin.Context) {
	var p req.Login
	if err := c.ShouldBind(&p); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    500,
			"message": "参数错误",
			"data":    err.Error(),
		})
		return
	}
	userLogin, err := global.UserClient.Login(c, &user.LoginReq{
		Phone:    p.Phone,
		Password: p.Password,
		SendCode: p.SendCode,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    500,
			"message": "参数错误11",
			"data":    err.Error(),
		})
		return
	}

	token, _ := pkg.NewJWT("2303a").CreateToken(pkg.CustomClaims{
		ID: uint(userLogin.Id),
		StandardClaims: jwt.StandardClaims{
			ExpiresAt: int64(time.Hour * 1),
		},
	})

	c.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "登录成功",
		"data":    token,
	})
}

// 注册
func Register(c *gin.Context) {

	var p req.Register
	if err := c.ShouldBind(&p); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    500,
			"message": "参数错误",
			"data":    err.Error(),
		})
		return
	}
	userRegister, err := global.UserClient.Register(c, &user.RegisterReq{
		Phone:    p.Phone,
		SendCode: p.SendCode,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    500,
			"message": "参数错误11",
			"data":    err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "注册成功",
		"data":    userRegister,
	})

}

// 短信验证
func SendSms(c *gin.Context) {

	var p req.SendSms
	if err := c.ShouldBind(&p); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    500,
			"message": "参数错误",
			"data":    err.Error(),
		})
		return
	}
	_, err := global.UserClient.SendSms(c, &user.SendSmsReq{
		Phone: p.Phone,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    500,
			"message": "参数错误11",
			"data":    err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "短信验证码发送成功",
		//"data":    sendSms,
	})
}

// 完善信息
func UserInfo(c *gin.Context) {

	var p req.UserInfo
	if err := c.ShouldBind(&p); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    500,
			"message": "参数错误",
			"data":    err.Error(),
		})
		return
	}
	userInfo, err := global.UserClient.UserInfo(c, &user.UserInfoReq{
		Id:       int64(p.Id),
		NickName: p.NickName,
		Avatar:   p.Avatar,
		RelaName: p.RelaName,
		Status:   p.Status,
		Balance:  float32(p.Balance),
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    500,
			"message": "参数错误11",
			"data":    err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "信息完善成功",
		"data":    userInfo,
	})
}

// 找回密码
func UserNewPassword(c *gin.Context) {

	var p req.UserNewPassword
	if err := c.ShouldBind(&p); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    500,
			"message": "参数错误",
			"data":    err.Error(),
		})
		return
	}
	password, err := global.UserClient.UserNewPassword(c, &user.UserNewPasswordReq{
		Phone:       p.Phone,
		Password:    p.Password,
		SendCode:    p.SendCode,
		NewPassword: p.NewPassword,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    500,
			"message": "参数错误11",
			"data":    err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "密码找回成功",
		"data":    password,
	})
}

// 用户首页
func UserIndex(c *gin.Context) {

	var p req.UserIndex
	if err := c.ShouldBind(&p); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    500,
			"message": "参数错误",
			"data":    err.Error(),
		})
		return
	}
	userIndex, err := global.UserClient.UserIndex(c, &user.UserIndexReq{
		Id: int64(p.Id),
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    500,
			"message": "参数错误11",
			"data":    err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "用户首页查询成功",
		"data":    userIndex,
	})
}
